# view.py
from PySide6.QtWidgets import QApplication, QMainWindow, QListView, QFrame, QLabel, QPushButton
from PySide6.QtCore import Qt, QAbstractListModel, QModelIndex
from time_model import TimeModel

class TimeDelegate(QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        pass  # 不需要编辑器，因为我们只是显示静态内容

    def paint(self, painter, option, index):
        time_str = index.data(Qt.DisplayRole)
        painter.drawText(option.rect, Qt.AlignCenter, time_str)

class TimeView(QMainWindow):
    def __init__(self):
        super().__init__()

        self.model = TimeModel()
        self.listView = QListView()
        self.listView.setItemDelegate(TimeDelegate())
        self.listView.setModel(self.model)

        layout = QVBoxLayout()
        frames = [QFrame() for _ in range(6)]
        for frame in frames:
            layout.addWidget(frame)
            label = QLabel(frame)
            label.setAlignment(Qt.AlignCenter)
            self.listView.setItemWidget(index_of_frame, label)  # 需要设置具体的index
            button = QPushButton("Start/Stop")
            button.clicked.connect(lambda checked, model=self.model: self.toggle_time(model))
            layout.addWidget(button)

        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

    def toggle_time(self, model):
        model.set_running(not model.is_running_)

